<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :pagination="false">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" @click="handleChoose" preIcon="ant-design:plus-outlined">引入指标</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <LabItemsTemp2CondModal @register="registerNormModal" @success="handleSuccess" />
    <!--选择实验指标-->
    <LabNormalModal @register="registerNormalModal" @mainReload="reload" :itemsId="props.itemsId" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="cond-labItemsTemp2Cond" setup>
  import { reactive, onBeforeUpdate } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columnsNorm } from '../LabWscientificItem.data';
  import { normList, normDelOne } from '../LabWscientificItem.api';
  import LabItemsTemp2CondModal from './LabNormModal.vue';
  import LabNormalModal from './labNormalM.vue';

  const props = defineProps({
    itemsId: { type: String },
  });

  const queryParam = reactive<any>({
    itemsId: props.itemsId,
  });

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '实验项目模板_检测条件',
      api: normList,
      columns: columnsNorm,
      canResize: false,
      useSearchForm: false,
      showIndexColumn: true,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        queryParam.itemsId = props.itemsId;
        return Object.assign(params, queryParam);
      },
    },
  });
  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

  const [registerNormModal, { openModal }] = useModal();
  const [registerNormalModal, { openModal: openNModal }] = useModal();

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, { itemsId: props.itemsId, flag: '1' });
  }
  /**
   * 点击引入指标事件回调
   */
  function handleChoose() {
    openNModal(true, { itemsId: props.itemsId });
  }
  // 数据更新重新加载表格数据
  onBeforeUpdate(() => {
    reload();
  });
  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, { record, flag: '2' });
  }

  /**
   * 详情
   */
  // function handleDetail(record) {
  //   registerModal.value.disableSubmit = true;
  //   registerModal.value.edit(record);
  // }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await normDelOne({ id: record.id }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }

    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }

    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
